package com.fairytask.engine.components.text
{
	import com.fairytask.data.BaseData;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	/**
	 * 只带纵向滚动条的文本组件 
	 * @author Hanjy
	 * 
	 */	
	public class Text extends BaseComponent
	{
		private 	var _txtSkin		:Sprite;
		
		private 	var _txt			:TextField;
		private 	var _tf				:TextFormat;
		private 	var _isScroll		:Boolean	=	false;
		
		private	var _pScrollSkin	:Sprite;
		
		private	var scrollPanel		:ScrollPanel;
		private	var sp				:Sprite;
		
		private 	var _scrollVal		:Number	=	0;
		
		private 	var initHeight		:uint;
		private 	var initWidth		:uint;
		private 	var _autoSize		:Boolean;//是否自适应 
		private var _color:String		=	"ffffff";
		
		private var data				:BaseData	=	new BaseData();
		
		public var isLink				:Boolean	=	false;
		//属性设置
		private var _txtFilters		:Array	=	[];
		//文字大小
		private var _txtSize			:int	=	12;
		
		public static var TEXTCOMPONT	:String	=	"TEXTCOMPONT";
		
		//html 链接 scrollpanel
		public function Text(w:uint,h:uint,vScrollSkin:String,hScrollSkin:String,hasScroll:Boolean=true,autoSize:Boolean=false,pSize:int=12)
		{
			super();
			_txtSize	=	pSize;
			initHeight	=	h;
			initWidth	=	w;
			_autoSize 	= 	autoSize;
			if(hasScroll)
			{
				init(w,h,new ScrollSkin(vScrollSkin, hScrollSkin));
			}
			else
			{
				init(w,h,null);
			}
		}
		
		/**
		 * 初始化显示 
		 * @param w
		 * @param h
		 * @param scrollSkin
		 * 
		 */
		private function init(w:uint,h:uint,scrollSkin:ScrollSkin):void
		{
			sp						=	new Sprite();
			_txt					=	new TextField();
			_tf						=	new TextFormat();
			_tf.font		=	"微软雅黑";
			_tf.size		=	_txtSize;
			_txt.defaultTextFormat	=	_tf;
			//_txt.name				=	TEXTCOMPONT;
			if(!_autoSize)
			{
				_txt.width				=	w;
				_txt.height				=	h;
				_txt.wordWrap			=	true;
			}
			_txt.multiline			=	true;
			_txt.autoSize			=	TextFieldAutoSize.LEFT;
			_txt.type				=	TextFieldType.DYNAMIC;
			_txt.selectable			=	false;
			_txt.mouseWheelEnabled	=	false;
			_txt.textColor			=	uint("0x"+_color);
			sp.addChild(_txt);
			if(null!=scrollSkin)
			{
				scrollPanel	=	new ScrollPanel(new Rectangle(0,0,w,h),sp,scrollSkin);
				addChild(scrollPanel);
				initEvent();
			}
			else
			{
				addChild(sp);
			}
		}
		
		private function  initText():void
		{
			
		}
		
		/**
		 * 初始化文字事件 
		 */
		private function initEvent():void
		{
			scrollPanel.addEventListener(MouseEvent.CLICK, onScrollClick);
		}
		
		/**
		 * 事件 
		 * @param evt
		 * 
		 */
		private function onScrollClick(evt:MouseEvent):void
		{
			//isLink	=	true;
		}
		
		/**
		 * 清除文字显示与文字区域 
		 */
		public function clear():void
		{
			_txt	=	new TextField();
			while(sp.numChildren>0)
			{
				sp.removeChildAt(0);
			}
			sp.addChild(_txt);
		}
		
		public function get txtWidth():int
		{
			return sp.width;
		}
		
		public function get txtHight():int
		{
			return sp.height;
		}
		
		public function set htmlStr(val:String):void
		{
			if(val!=null)
			{
				if(_autoSize)
				{
					_txt.wordWrap = false;
				}
				_txt.htmlText	=	val;
			}
			if(_autoSize)
			{
				if(_txt.width > initWidth)
				{
					_txt.wordWrap = true;
					_txt.width = initWidth;
				}
			}
			if(sp.height>initHeight)
			{
				_txt.width	=	initWidth-15;
			}
			onTextChange();
		}
		
		public function set styleSheet(val:StyleSheet):void
		{
			_txt.styleSheet	=	val;
		}
		
		public function get styleSheet():StyleSheet
		{
			return _txt.styleSheet;
		}
		
		public function get htmlStr():String
		{
			return _txt.htmlText;
		}
		
		/**
		 * 移除文字和事件 
		 */
		public function remove():void
		{
			_txt.removeEventListener(TextEvent.LINK,onLinkHandle);
			data.DeleteProperties("onLinkHandle");
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		/**
		 * 文字链接点击事件 
		 * @param val
		 */
		public function addLinkClickHandel(val:Function):void
		{
			data.HandleProperties("onLinkHandle",val);
			_txt.addEventListener(TextEvent.LINK,onLinkHandle);
		}
		
		public function removeLinkClickHandel(val:Function):void
		{
			_txt.removeEventListener(TextEvent.LINK,onLinkHandle);
		}
		
		public function onTextChange():void
		{
			if(null!=scrollPanel&&_scrollVal!=-1)
			{
				scrollPanel.update(_scrollVal);
			}
		}
		
		public function hideScroll():void
		{
			if(null!=scrollPanel)
			{
				scrollPanel.hideScrollBar();
			}
		}
		public function showScroll():void
		{
			if(null!=scrollPanel)
			{
				scrollPanel.showScrollBar(true);
			}
		}
		
		private function onLinkHandle(e:TextEvent):void
		{
			var tFunc:Function;
			
			if(data.CheckProperties("onLinkHandle"))
			{
				isLink	=	true;
				tFunc	=	data.GetProperties("onLinkHandle") as Function;
				tFunc.call(this,e.text);
				tFunc	=	null;
			}
		}
		
		public function setFilter():void
		{
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	6;
			glow.quality			= 	BitmapFilterQuality.LOW;
			_txt.filters		=	[glow];
		}

		//=======================================================================
		//系统自动生成
		/**
		 * 设定滚动条位置 
		 * @param value
		 */
		public function set scrollVal(value:Number):void
		{
			_scrollVal = value;
		}
		
		/**
		 * 设置文字格式 
		 * @param textFormat
		 * 
		 */
		public function set textFormat(textFormat:TextFormat):void
		{
			_txt.setTextFormat(textFormat);
			_txt.defaultTextFormat = textFormat;
		}
		
		/**
		 * 取得文字基础颜色 
		 * @return 
		 */
		public function get color():String
		{
			return _color;
		}
		
		/**
		 * 设定文字颜色 
		 * @param value
		 * 
		 */
		public function set color(value:String):void
		{
			_color	= value;
			_txt.textColor =	uint("0x"+value);
			_txt.htmlText = _txt.htmlText;
		}
		
		/**
		 * 取得滤镜 
		 * @return 
		 */
		public function get txtFilters():Array
		{
			return _txtFilters;
		}

		/**
		 * 设定文字滤镜 
		 * @param value
		 * 
		 */
		public function set txtFilters(value:Array):void
		{ 
			_txtFilters = value;
			_txt.filters	=	value;
		}
		
		/**
		 * 设置文字名称，用于场景 
		 * @param val
		 * 
		 */
		public function set txtName(val:String):void
		{
			_txt.name	=	val;
		}
	}
}